package com.example.demo.leetcode.top;


import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;

/**
 *
 * 层序遍历
 *
 */
public class TierSort {


    public List<List<Integer>> levelOrder(TreeNode root) {

        Queue<TreeNode> queue = new LinkedList<>();
        queue.offer(root);
        List<List<Integer>> res = new ArrayList<>();
        while(!queue.isEmpty()){
            int size =queue.size();
            List<Integer> list = new ArrayList<>();
            for(int i=0;i<size;i++){
                TreeNode node = queue.poll();
                if(node==null){
                    continue;
                }
                queue.add(node.left);
                queue.add(node.right);
                list.add(node.val);
            }
            if(!list.isEmpty()){
                res.add(list);
            }
        }
        return res;
    }




    public static void main(String[] args) {

        Queue<String> queue = new LinkedList<>();
        queue.offer("");
        String peek = queue.peek();//拿到链表中的首个
        String poll = queue.poll();//拿到链表中的首个，并且移除

    }




  public class TreeNode {
      int val;
      TreeNode left;
      TreeNode right;
      TreeNode(int x) { val = x; }
  }

}
